package com.changgou.seckill.mq;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 秒杀订单延时回滚
 */
@Configuration
public class QueueConfig {
    /***
     * 延时队列消息过期的时候会将消息发给orderListenerQuery
     * @return
     */
    @Bean
    public Queue orderDelaySecillQueue(){
        /**
         * 死信队列
         * 具体的实现思路
         *
         */
        //死信交换机
        return QueueBuilder
                .durable("orderDelaySecillQueue")
                .withArgument("x-dead-letter-exchange", "orderListenerSecillExchange")
                .withArgument("x-dead-letter-routing-key","orderListenerSecillQuery")
                .build();
    }

    /**
     * 队列2作用时用来接收队列的消息
     * @return @Bean(value="orderListenerQuery") 不写value默认的是方法的名字
     */
    @Bean
    public Queue orderListenerSecillQuery(){
        return  new Queue("orderListenerSecillQuery",true);
    }
    /**
     * 交换机
     */
    @Bean
    public Exchange orderListenerSecillExchange(){
        return  new DirectExchange("orderListenerSecillExchange");
    }
    //队列2orderListenerQuery绑定到交换机上
    @Bean
    public Binding orderListenerBind(Queue orderListenerSecillQuery, Exchange orderListenerSecillExchange){
        return  BindingBuilder.bind(orderListenerSecillQuery).
                to(orderListenerSecillExchange)
                .with("orderListenerSecillQuery")
                .noargs();
    }

}
